/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * All Rights Researved.
 *
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *
 *
 * @author: jin Chen 
 * @date: Sep 17, 2003$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.toolkits;

import edu.psu.geovista.app.pcp.plot.services.CommonService;
import edu.psu.geovista.app.pcp.toolkits.Math.JMath;
import edu.psu.geovista.app.pcp.toolkits.group.DataIDSet;
import edu.psu.geovista.classification.Category;
import edu.psu.geovista.classification.CategoryItf;
import edu.psu.geovista.classification.CategoryList;
import edu.psu.geovista.data.model.AppDataModel;

import java.util.HashSet;

public class Summarizer {
    public static final boolean DEBUG=false;
   /**
    * Given cGenerate a model which
    * @param ctgs
    * @param data
    * @return  a datamodel which contain the category medians of each column.  
    *           
    */
    public static AppDataModel getCategorySummary(edu.psu.geovista.data.model.AppDataModel data,CategoryItf[] ctgs){
       AppDataModel mm=edu.psu.geovista.data.model.DefaultPlotModel.createDefaultPlotModel();//Median Model
       String[] names=data.getColumnNames();
       mm.setColumnNames(names);

       for (int i=0;i<ctgs.length ;i++){
           CategoryItf ctg=ctgs[i];

           HashSet obIDs=ctg.getMemberIds() ;
           if(obIDs!=null&&obIDs.size() >0){
            float[] aRowOfmedians=CommonService.generateMedianRecord((AppDataModel)data,obIDs);//a record contain the median values for the state
               String id = ctg.getID().toString();
               mm.addRow(aRowOfmedians,id );// 

           }

       }

        return mm;
    }
    /**
     * Get median of each dataGroup
     * @param data  a column data
     * @param dgs  the column is classified into the given datagroup.
     *              The classification don't have to be made based on the column
     * @return      Array whose element is the median value of each datagroup
     *               
     *                 thus matach to the corresponding category ID
     */
    public static float[] getMedianSummary(float[] data,DataIDSet[] dgs){
        if (DEBUG){
             System.out.println("raw Data:"+showData(data)+" is classified into "+dgs.length +" group->");
        }
         //DataGroup[] dgs=getIDGroup(ctgs); // 
         float[] medians=new float[dgs.length ];// 
         for (int i=0;i<dgs.length ;i++){
             int[] ids=dgs[i].getIndice() ; // 
             float[] groupValues=new float[ids.length ];// 
             for (int j=0;j<ids.length ;j++){
                 groupValues[j]=data[ids[j]];
             }
             //dp{
             if (DEBUG){
                 System.out.println("group"+i+":"+showData(groupValues));
             }//dp}
             try {
                 if(groupValues==null||groupValues.length <=0)
                       medians[i]=Float.NaN ;
                 else
                        medians[i]=JMath.calculateMedian(groupValues);
             } catch (Exception e) {
                 medians[i]=Float.NaN ;
                 e.printStackTrace();  //To change body of catch statement use Options | File Templates.
                 //log
             }
         }
         //dp{
         if (DEBUG){
             System.out.println("raw Data:"+showData(data)+"'s median:");
             System.out.println(""+showData(medians));
             System.out.println("<--");

         }//dp}
         //System.out.println("");
         return medians;

    }
    private static String showData(float[] data){
         String s="";
         for (int i=0;data!=null&&i<data.length ;i++){
             s=s+data[i]+",";
         }
         return s;

    }
    /**
     *
     * @param ctgs  category list
     * @return      Array of datagroup, each datagroup contain same member ID as the corresponding category
     *               
     */
    public static DataIDSet[] getIDGroup(CategoryList ctgs){
        //Hashtable ctgList=ctgs.getCtgList() ;
        //Enumeration e=ctgList.elements();
        DataIDSet[] dataG=new DataIDSet[ctgs.size() ];

        for (int i=0;i<ctgs.size() ;i++){
            // 
            Category category = (Category) ctgs.getCategoryByID(i);
            HashSet IDs=category.getMemberIds();
            DataIDSet dg=null;
            if(IDs!=null&&IDs.size() >0){
                dg=new DataIDSet(IDs);
            }
            else{
                dg=new DataIDSet(new int[0]);
            }
            dataG[i]=dg;
        }
        return dataG;
    }


}
